$(function () {
    let id = localStorage.getItem("newsId");
    findNewsClassify();
    selectNewsById(id);
})

//查询一条数据库
function selectNewsById(id) {
    $.ajax({
        data: {id: id},
        url: '/back/news/updateNews',
        dataType: 'json',
        type: 'get',
        success: function (data) {
            setData(data.newsModel);
        }
    })
}

function setData(data) {
    $("#newsClassify").val(data.type);
    $("#title").val(data.title);
    $("#content").val(data.content);
    $("#newsTime").val(data.time);
}

function findNewsClassify() {
    $.ajax({
        data: '',
        dataType: 'json',
        type: 'get',
        url: '/back/news/findNewsClassify',
        success: function (data) {
            data = data.model;
            console.log(data);
            let html = '';
            for (let i = 0; i < data.length; i++) {
                html += ' <option value="' + data[i].id + '">' + data[i].classify + '</option>';
            }
            ;
            $("#newsClassify").html(html);
        }
    })
}

//更新数据===================================================
function updateNews() {
    let newsid = localStorage.getItem("newsId");
    updateNewsById(newsid);
}

function updateNewsById(id) {
    let data = {
        id:id,
        newsClassify:$("#newsClassify").val(),
        title:$("#title").val(),
        content:$("#content").val(),
        newsTime:$("#newsTime").val(),
    };
    $.ajax({
        data: data,
        url:'/back/news/updateNews',
        dataType: 'json',
        type: 'post',
        success:function (data) {
            if (data.result==1){
                alert("更新成功");
            }else {
                alert("更新失败");
            };
        }
    });

}

//返回上一页面。。。。。。。。
function back() {
    $("#right").load("/back/html/news/newsManage.html");
}
